/**
 * 
 */
package com.maywide.jdp.system.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.maywide.jdp.core.vo.KeyValue;
import com.maywide.jdp.core.vo.Page;
import com.maywide.jdp.system.annotation.JSON;
import com.maywide.jdp.system.bean.DomainBean;
import com.maywide.jdp.system.entities.Domain;
import com.maywide.jdp.system.utils.JSONEditor;
import com.maywide.jdp.system.vo.DomainTree;

/**
 * 数据区域管理Service
 * 
 * @author luoyang
 * @version 1.0
 * @since JDK1.6
 */
@Controller("domainService")
public class DomainServiceImpl implements DomainService {
	/**
	 * 数据区域Bean接口
	 */
	private DomainBean bean = null;

	/**
	 * @param bean
	 *            the bean to set
	 */
	@Autowired
	@Qualifier("domainBean")
	public void setBean(DomainBean bean) {
		this.bean = bean;
	}

	@JSON
	@RequestMapping(value = "/system/domain/insert.json")
	@Override
	public Domain insert(@RequestParam(value = "domain")Domain domain, HttpServletRequest request,
			HttpServletResponse response, HttpSession session)
			throws Exception {
		bean.insert(domain);
		return domain;
	}

	@JSON
	@RequestMapping(value = "/system/domain/update.json")
	@Override
	public Domain update(@RequestParam(value = "domain")Domain domainVO, HttpServletRequest request,
			HttpServletResponse response, HttpSession session)
			throws Exception {
		String id = domainVO.getId();
		Domain domain = (Domain)bean.load(id);
		//取得不可改变属性
		Domain parent = domain.getParent();
		
		domainVO.setParent(parent);
		BeanUtils.copyProperties(domainVO, domain);
		
		bean.update(domain);
		return domain;
	}

	@JSON
	@RequestMapping(value = "/system/domain/load.json")
	@Override
	public Domain load(@RequestParam(value = "id")String id, HttpServletRequest request,
			HttpServletResponse response, HttpSession session)
			throws Exception {
		Domain domain = null;
		domain = (Domain) bean.load(id);
		return domain;
	}

	@RequestMapping(value = "/system/domain/lock.json")
	@Override
	public void lock(@RequestParam(value = "id")String id, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception {
		bean.lock(id);
	}

	@RequestMapping(value = "/system/domain/unlock.json")
	@Override
	public void unlock(@RequestParam(value = "id")String id, HttpServletRequest request,
			HttpServletResponse response, HttpSession session)
			throws Exception {
		bean.unlock(id);
	}

	@RequestMapping(value = "/system/domain/move.json")
	@Override
	public void move(
			@RequestParam(value = "ids")List<String> ids, 
			@RequestParam(value = "targetID")String parentID, 
			HttpServletRequest request,
			HttpServletResponse response, 
			HttpSession session)
			throws Exception {
		bean.move(ids, parentID);
	}

	@JSON
	@RequestMapping(value = "/system/domain/combo.json")
	public List<KeyValue> combo(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws Exception {
		List<KeyValue> keyValues = new ArrayList<KeyValue>();
		keyValues.add(new KeyValue("D","DEFAULT"));
		keyValues.add(new KeyValue("H","HQL"));
		keyValues.add(new KeyValue("S","SQL"));
		keyValues.add(new KeyValue("J","JSON"));
		return keyValues;
	}

	@JSON
	@RequestMapping(value = "/system/domain/tree.json")
	@SuppressWarnings("unchecked")
	public List<DomainTree> tree(@RequestParam(value = "parentID")String parentID,HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws Exception {
		Map<String,Object> conditions = new HashMap<String,Object>();
		
		if (parentID != null && !"domain_root".equals(parentID) && !"dir_domain_root".equals(parentID))
			conditions.put("parentID", parentID);
		else
			conditions.put("noParentID", null);
		
		return bean.tree(conditions);
	}

	@JSON
	@RequestMapping(value = "/system/domain/page.json")
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Page page(
			@RequestParam(value = "first", defaultValue = "0") int first,
			@RequestParam(value = "limit", defaultValue = "20") int limit,
			@RequestParam(value = "sort",required = false)String sort, 
			@RequestParam(value = "dir",required = false)String dir,
			@RequestParam(value = "parentID",required = false)String parentID,
			@RequestParam(value = "key",required = false)String key,
			@RequestParam(value = "active",required = false)String active,
			HttpServletRequest request,
			HttpServletResponse response, 
			HttpSession session)
			throws Exception {
		Map conditions = new HashMap();
		
		if(key != null) {
			key = new StringBuffer("%").append(key).append("%").toString();
			conditions.put("key", key);
		}
		
		if(active != null)
			conditions.put("active", Boolean.parseBoolean(active));
		
		if (parentID != null && !"domain_root".equals(parentID))
			conditions.put("parentID", parentID);
		else
			conditions.put("noParentID", null);
		
		return bean.page(first, limit, sort, dir, conditions, null);
	}

	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Domain.class, new JSONEditor(Domain.class));
	}

}
